
/*
	### 解析并显示已开通城市到元素中
	参一：开通城市数据的json对象 (省包含市集合，市包含县集合)
		数据格式示例：
			[
				{
					"id":"1",
					...
					"city":[
						{
							"id":"35",
							...
							"county":[
								{
									"id":"381",
									...
								},
								......
							]
						},
						......
					]
				},
				......
			]
	参二：省的select元素id
	参三：市的select元素id
	参四：县的select元素id
	参五：回调函数，返回选择的县信息
	示例：

		<select id="province"></select>
		<select id="city"></select>
		<select id="county"></select>

		var city_info = JSON.parse(res);
		ViewOpenCity(city_info,province,city,county,function(county_info){
			C(county_info);
			结果：
			{"id":"2832","code":"610103","name":"碑林区","parent_id":"328","level":"2","db_host":"51136888856","db_name":"wuye1"}
		});
*/
function ViewOpenCity(city_info,province,city,county,fun){
	var province_obj = '';
	var city_obj = '';
	var county_obj = '';

	ViewProvince(city_info,province,city,county);

	function ViewProvince(city_info,province,city,county){
		var province_str = '';
		for(var i=0;i<city_info.length;i++){
			if(localStorage.province_index && localStorage.province_index == i){
				province_str += "<option selected>"+city_info[i].name+"</option>";
			}else{
				province_str += "<option>"+city_info[i].name+"</option>";
			}
		}
		province.innerHTML = province_str;
		ViewCity(city_info,province,city,county);
		province.onchange=function(){
			//省切换时重置市和县的下标
			localStorage.city_index = '';
			localStorage.county_index = '';
			ViewCity(city_info,province,city,county);
		}
	}


	function ViewCity(city_info,province,city,county){
		var province_index = $(province).find('option:selected').index();
		localStorage.province_index = province_index;	//保存省下标
		province_obj = city_info[province_index];
		var city_str = '';
		for(var i=0;i<province_obj.city.length;i++){
			if(localStorage.city_index && localStorage.city_index == i){
				city_str += "<option selected>"+province_obj.city[i].name+"</option>";
			}else{
				city_str += "<option>"+province_obj.city[i].name+"</option>";
			}
		}
		city.innerHTML = city_str;
		ViewCounty(city_info,province,city,county);
		city.onchange=function(){
			//市切换时重置县的下标
			localStorage.county_index = '';
			ViewCounty(city_info,province,city,county);
		}
	}


	function ViewCounty(city_info,province,city,county){
		var city_index = $(city).find('option:selected').index();
		localStorage.city_index = city_index;	//保存市下标
		city_obj = province_obj.city[city_index];
		var county_str = '';
		for(var i=0;i<city_obj.county.length;i++){
			if(localStorage.county_index && localStorage.county_index == i){
				county_str += "<option selected value='"+JSON.stringify(city_obj.county[i])+"'>"+city_obj.county[i].name+"</option>";
			}else{
				county_str += "<option value='"+JSON.stringify(city_obj.county[i])+"'>"+city_obj.county[i].name+"</option>";
			}
		}
		county.innerHTML = county_str;
		SessCountyInfo(county,fun);
		county.onchange=function(){
			SessCountyInfo(county,fun);
		}
	}
	function SessCountyInfo(county,fun){
		var county_index = $(county).find('option:selected').index();
		county_obj = city_obj.county[county_index];
		localStorage.county_index = county_index;	//保存县下标
		if(fun){
			fun($(county).find('option:selected').val());
		}
		//localStorage.current_county_info = $(county).find('option:selected').val();
	}
}